package ai;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestAIastar {

	static String fileName = "aiP1_input2-ansi.txt";
	static File file = new File(fileName);
	static boolean distanceMode = false;

	static List<City> cities = new ArrayList<City>();
	static List<WeightedEdge> edges = new ArrayList<WeightedEdge>();

	public static void processLine(String line) {
		// if the line starts with a # then it is a comment, skip it
		if (line.subSequence(0, 1).equals("#")) {
			// nothing happens here.
		}
		// if the line starts with --- then it is time to switch to distance
		// mode
		else if (line.equals("---")) {
			// now in distance mode
			distanceMode = true;
		}
		// otherwise, if it is not yet distance mode, collect the lat and long
		// for
		// each city and store it in the list of cities
		else if (distanceMode == false) {
			Scanner scanner = new Scanner(line);

			scanner.useDelimiter("-");

			String cityName = scanner.next().trim();
			String latitude = scanner.next().trim();
			String longitude = scanner.next().trim();
			// currently not looking for duplicate cities

			City newCity = new City(cityName, latitude, longitude);
			cities.add(newCity);
		}
		// this happens when distanceMode == true, start adding edges
		// between cities.
		else {
			Scanner scanner = new Scanner(line);

			scanner.useDelimiter("-");

			String startCity = scanner.next().trim();
			String endCity = scanner.next().trim();
			String distance = scanner.next().trim();

			int startNodeLocation = findCity(startCity);
			int endNodeLocation = findCity(endCity);

			if (startNodeLocation == -1) {
				// System.out.println("Start city of: " + startCity +
				// " not in list.");
				System.out.println("Error!  Start city of: " + startCity);
				System.out
						.println("not in list of cities while creating edges.");
			}
			if (endNodeLocation == -1) {
				// System.out.println("End city of: " + endCity +
				// " not in list.");
				System.out.println("Error!  End city of: " + endCity);
				System.out
						.println("not in list of cities while creating edges.");
			}
			edges.add(new WeightedEdge(startNodeLocation, endNodeLocation,
					Integer.parseInt(distance)));
		}
	}

	private static int findCity(String cityName) {
		int vectorNumber = -1;
		int citiesSize = cities.size();
		for (int i = 0; i < citiesSize; i++) {
			if (cities.get(i).getCityName().equals(cityName)) {
				vectorNumber = i;
			}
		}
		return vectorNumber;
	}

	private static int findCity(ArrayList<City> externalCities, String cityName) {
		int vectorNumber = -1;
		int externalCitiesSize = externalCities.size();
		for (int i = 0; i < externalCitiesSize; i++) {
			if (externalCities.get(i).getCityName().equals(cityName))
				;
			vectorNumber = i;
		}
		return vectorNumber;
	}

	public static void main(String[] args) throws FileNotFoundException {

		Scanner scanner = new Scanner(file);

		while (scanner.hasNext()) {
			processLine(scanner.nextLine());
		}
		

		AIGraph<City> aStarGraph = new AIGraph<City>(edges, cities);
		System.out.println(aStarGraph.AIastar("Baton Rouge", "Poplar Bluff"));
	}
}
